package com.yuyou.tas.feedback.service.impl;

import com.yuyou.tas.feedback.mapper.FeedbackNoteMapper;
import com.yuyou.tas.feedback.service.IFeedbackNoteService;
import com.yuyou.tas.feedback.service.IFeedbackService;
import com.yuyou.tas.service.api.feedback.entity.FeedbackNote;
import com.yuyou.tas.service.api.feedback.vo.resp.FeedbackNoteMoreVo;
import com.yuyou.tas.util.bean.ResBoolSimpleInfo;
import com.yuyou.tas.util.bean.ReturnResult;
import com.yuyou.tas.util.enums.feedback.MediaOperateEnums;
import com.yuyou.tas.util.tool.Sequence;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FeedbackNoteServiceImpl implements IFeedbackNoteService {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private FeedbackNoteMapper mapper;

    @Autowired
    private IFeedbackService feedbackService;

    @Override
    public int deleteByPrimaryKey(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(FeedbackNote record) {
        return mapper.insert(record);
    }

    @Override
    public FeedbackNote selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(FeedbackNote record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    @Transactional
    public ResBoolSimpleInfo addFeedbackNote(Long feedbackClassId, String urls, Long organId, Long userId) {
        if(!StringUtils.isEmpty(feedbackClassId)){
            mapper.delByFeedbackClass(feedbackClassId);//删除旧数据
            Integer sort = 0;
            if(!StringUtils.isEmpty(urls)){
                String[] url_Array = urls.split(";");
                List<FeedbackNote> needOperate = new ArrayList<>();
                for (int i = 0; i < url_Array.length ; i ++){
                    if(!StringUtils.isEmpty(url_Array[i])){
                        FeedbackNote note = new FeedbackNote();
                        note.setAuthOrganizationId(organId);
                        note.setCreateTime(new Date());
                        note.setFeedbackClassId(feedbackClassId);
                        note.setCreateUser(userId);
                        note.setId(Sequence.getSequence().nextId());
                        note.setSort(++sort);
                        if(url_Array[i].contains(",")){//微信的图片
                            String[] wetcatMedis = url_Array[i].split(",");
                            if(wetcatMedis.length < 2){
                                continue;
                            }
                            note.setWeixinMediaId(wetcatMedis[0]);
                            note.setMedia(wetcatMedis[1]);
                            note.setStatus(MediaOperateEnums.status.operating.getValue());
                            needOperate.add(note);
                        }else {//上传到七牛云的图片
                            note.setMedia(url_Array[i]);
                            note.setStatus(MediaOperateEnums.status.needNot.getValue());
                        }

                        mapper.insert(note);
                    }
                }
                //将需要处理的图片插入job表
                feedbackService.insert2Job(null , needOperate);
            }

            //未上传过的图片
//            if(!StringUtils.isEmpty(urls)){
//                String[] url_Array = urls.split(",");
//                try {
//                    String[] new_urls = callableService.completionServiceCount(url_Array , "image");
//
//                    for (int i = 0; i < new_urls.length ; i ++){
//                        if(!StringUtils.isEmpty(new_urls[i])){
//                            FeedbackNote note = new FeedbackNote();
//                            note.setAuthOrganizationId(organId);
//                            note.setCreateTime(new Date());
//                            note.setFeedbackClassId(feedbackClassId);
//                            note.setCreateUser(userId);
//                            note.setMedia(new_urls[i]);
//                            note.setId(Sequence.getSequence().nextId());
//                            note.setSort(++sort);
//
//                            mapper.insert(note);
//                        }
//                    }
//                } catch (Exception e) {
//                    logger.error("微信图片下载服务异常：" , e.getMessage());
//                    return new ResBoolSimpleInfo(false , "图片上传异常，请联系管理员");
//                }
//            }
        }
        return new ResBoolSimpleInfo(true , null);
    }

    @Override
    public ReturnResult delFeedbackNote(Long feedbackNoteId) {
        FeedbackNote note = mapper.selectByPrimaryKey(feedbackNoteId);
        if(note != null){
            mapper.deleteByPrimaryKey(note.getId());
        }
        return ReturnResult.success();
    }

    @Override
    public List<FeedbackNoteMoreVo> findByFeedClassId(Long feedbackClassId) {
        return mapper.findByFeedClassId(feedbackClassId);
    }

    @Override
    public List<FeedbackNote> findAllNeedOperate() {
        return mapper.findAllNeedOperate();
    }
}